using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Triggers
{
    [Serializable]
    public class HotFolderOutput:ResultBase
    {
        private string _FilePath;

        public string FilePath
        {
            get { return _FilePath; }
            set { _FilePath = value; }
        }

        private FileChangeType _ChangeType;

        public FileChangeType ChangeType
        {
            get { return _ChangeType; }
            set { _ChangeType = value; }
        }
	
        public HotFolderOutput():base(Guid.Empty,Guid.Empty)
        {
            this._FilePath = string.Empty;
            this._ChangeType = FileChangeType.Update;
        }

        public HotFolderOutput(Guid componentID, Guid jobTicketID, string filePath, FileChangeType changeType) : base(componentID, jobTicketID)
        {
            this._FilePath = filePath;
            this._ChangeType = changeType;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Trigger");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilePath", this.FilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"FileChangeType", ((int)this.ChangeType).ToString());

            return xNode;
        }

        public override ResultBase Instantiate(XmlNode xNode)
        {
            HotFolderOutput hotFile = new HotFolderOutput();
            string typeName = xNode.Attributes.GetNamedItem("Type").Value;
            if (typeName == this.GetType().FullName)
            {
                hotFile.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                hotFile.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                hotFile.FilePath = xNode.Attributes.GetNamedItem("FilePath").Value;
                hotFile.ChangeType = (FileChangeType) int.Parse(xNode.Attributes.GetNamedItem("FileChangeType").Value);

                return hotFile;
            }
            else
            {
                throw new Exception("Invalid type to deserialize from xml doc");
            }
        }
    }
}
